package com.mallbook.model.service;

import com.mallbook.model.exception.AccountExitsException;
import com.mallbook.model.exception.ServiceException;
import com.mallbook.model.pojo.Manager;
import com.mallbook.model.mapper.ManagerMapper;
import com.mallbook.model.utils.MD5Utils;
import com.mallbook.model.utils.MybatisUtils;
import org.apache.ibatis.exceptions.PersistenceException;

import java.util.List;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-06-25
 * 管理员service层
 */
public class ManagerService {


    /**
     * 新增管理员
     *
     * @param name 账号
     * @param pwd  密码
     */
    public void register(String name, String pwd) {
          ManagerMapper managerMapper = MybatisUtils.getMappers(ManagerMapper.class);
        Manager manager = new Manager();
        manager.setUsername(name);
        manager.setPassword(pwd);
        manager.setState(1);
        try {
            int managerNum = managerMapper.findManagerNumByName(manager.getUsername());
            if (managerNum != 0) {
                throw new AccountExitsException("账号已存在");
            }
            manager.setPassword(MD5Utils.stringToMD5(manager.getPassword()));
            managerMapper.addManager(manager);
        } catch (
                PersistenceException e) {
            e.printStackTrace();
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 删除管理员（逻辑删除）
     *
     * @param id 管理员ID
     */
    public void delManager(Integer id) {
        ManagerMapper managerMapper = MybatisUtils.getMappers(ManagerMapper.class);
        Manager manager = new Manager();
        manager.setState(-1);
        try {
            managerMapper.update(manager, id);
        } catch (
                PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 修改（修改密码和状态，IP 以及退出时间）
     *
     * @param pwd     密码
     * @param state   状态
     * @param ip      最后登录IP
     * @param endTime 最后登录时间
     * @param id      账号ID
     */
    public void updateManager(String pwd, Integer state, String ip, String endTime, Integer id) {
        ManagerMapper managerMapper = MybatisUtils.getMappers(ManagerMapper.class);
        Manager manager = new Manager();
        manager.setPassword(MD5Utils.stringToMD5(pwd));
        manager.setState(state);
        manager.setLastIp(ip);
        manager.setEndTime(endTime);
        try {
            managerMapper.update(manager, id);
        } catch (
                PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 登录
     *
     * @param username 账号
     * @param password 密码
     * @return
     */
    public Manager login(String username, String password) {
        ManagerMapper managerMapper = MybatisUtils.getMappers(ManagerMapper.class);
        try {
            return managerMapper.findManagerByNameAndPwd(username, MD5Utils.stringToMD5(password));
        } catch (
                PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 所有管理员
     *
     * @return
     */
    public List<Manager> getAllManagers() {
        ManagerMapper managerMapper = MybatisUtils.getMappers(ManagerMapper.class);
        try {
            return managerMapper.findManagers();
        } catch (
                PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }
}
